<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>222</title>
</head>
<body>
<div id="msg"></div>
<textarea name="" id="tt" cols="30" rows="10"></textarea>
<button id="btn">发送</button>

<script>
    const msg = document.getElementById('msg');
    let time = 1;
    // if (!window.WebSocket) {
    const ws = new WebSocket('ws://10.60.6.111:3000', 2);
    ws.onopen = ()=>{
        console.log('服务器连接成功')
        msg.innerHTML = '服务器连接成功'
    }
    ws.onclose = () => {
        console.log('服务器关闭连接')
        msg.innerHTML = '服务器关闭连接'
    };
    ws.onmessage = (e) => {
        var p = document.createElement('p');
        var node = document.createTextNode(e.data);
        p.appendChild(node);
        msg.appendChild(p);
    };
    document.getElementById('btn').addEventListener('click', (e) => {
        var text = document.getElementById('tt').value;
        ws.send(JSON.stringify({msg:text,to:'-1-3-'}))
        document.getElementById('tt').value = '';
    }, false);

    // }
</script>
</body>
</html>